package cn.xiaobai.http;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @Author yangdaji
 * @Date 2023/11/29
 * @PackageName cn.xiaobai.http
 * @ClassName Request
 */
public class Response {

    private OutputStream outputStream;

    private ResponseHeader responseHeader;

    private String body;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
        responseHeader = new ResponseHeader(200);
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    private String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void setCode(int code) {
        responseHeader.setCode(code);
        responseHeader.setMsg("not found");
    }

    public void write() throws IOException {
        String body = responseHeader + getBody();

        outputStream.write(body.getBytes());
        outputStream.flush();
        outputStream.close();

    }
}
